<template>
  <div>
    <el-card style="width: 50%">
      <el-form v-model="user" label-width="80px" style="padding-right: 20px">
        <div style="text-align: center;margin: 20px">
          <el-upload
              class="avatar-uploader"
              :action="$baseUrl+'/file/upload'"
              :headers={token:user.token}
              :on-success="handleAvatarSuccess"
              :show-file-list="false"
          >
            <img v-if="user.avatar" :src="user.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
        <el-form-item label="用户名" prop="username" >
          <el-input v-model="user.username" disabled placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name" >
          <el-input v-model="user.name"  placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone" >
          <el-input v-model="user.phone"  placeholder="手机号码"></el-input>
        </el-form-item>
        <el-form-item label="邮箱地址" prop="email" >
          <el-input v-model="user.email"  placeholder="邮箱地址"></el-input>
        </el-form-item>
        <el-form-item label="住址" prop="address" >
          <el-input v-model="user.address"  placeholder="address"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role" >
          <el-radio-group v-model="user.role">
            <el-radio label="用户"></el-radio>
            <el-radio label="商家"></el-radio>
          </el-radio-group>
        </el-form-item>
        <div style="text-align: center;margin-bottom: 20px">
          <el-button type="primary" @click="update">保 存</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "Person",
  data(){
    return {
      user:JSON.parse(localStorage.getItem("user") || '{}')
    }
  },
  methods:{
    update(){
      this.$request.post('/user/update',this.user).then(res=>{
        if (res.code==='200'){
          this.$message.success('保存数据成功')
          //更新浏览器缓存数据
          localStorage.setItem("user",JSON.stringify(this.user))
          this.$emit('update:user',this.user) //更新组件的user对象
        }else {
          this.$message.error(res.msg)
        }
      })
    },
    handleAvatarSuccess(response,file,fileList){
      console.log(response,file)
      this.user.avatar=response.data
    }
  }
}
</script>

<style scoped>
/deep/.el-form-item__label{
  font-weight: bold;
}
/deep/.el-upload{
  border-radius: 50%;
}
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
/deep/ .avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
/deep/ .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
  border-radius: 50%;
}
</style>